package com.sky.service.admin.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishPageQueryDTO;
import com.sky.mapper.admin.DishFlavorsMapper;
import com.sky.mapper.admin.DishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.admin.Dishservice;
import com.sky.utils.ThreadLocalUtils;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DishserviceImpl implements Dishservice {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Override
    public PageResult findByPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        List<Dish> dishList = dishMapper.findByPage(dishPageQueryDTO);
        Page<Dish> pageObj = (Page<Dish>) dishList;
        return new PageResult(pageObj.getTotal(),pageObj.getResult());
    }

    //TODO 细节有待完善
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDish(Dish dish) {
        dish.setCreateTime(LocalDateTime.now());
        dish.setCreateUser(ThreadLocalUtils.getLoginEmpId());
        dishMapper.insertDish(dish);
        List<DishFlavor> flavors = dish.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> flavor.setDishId(dish.getId()));
            dishFlavorsMapper.insertFlavor(flavors);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(List<Long> ids) {
        dishMapper.deleteByIds(ids);
        dishFlavorsMapper.delectFlavor(ids);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Dish dish) {
        dish.setUpdateUser(ThreadLocalUtils.getLoginEmpId());
        dish.setUpdateTime(LocalDateTime.now());
        dishMapper.updateDish(dish);
        List<Long> dishIds = Arrays.asList(dish.getId());  //注意
        dishFlavorsMapper.delectFlavor(dishIds);
        if(!CollectionUtil.isEmpty(dish.getFlavors())){  //注意
            dish.getFlavors().forEach(flavor -> flavor.setDishId(dish.getId()));
            dishFlavorsMapper.insertFlavor(dish.getFlavors());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public DishVO findDishById(Long id) {
        Dish dish = dishMapper.findDishById(id);
        List<DishFlavor> flavors = dishFlavorsMapper.findFlavorByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    @Override
    public List<Dish> listByCategoryId(Integer categoryId) {
        return dishMapper.findByCategoryId(categoryId);
    }
}
